﻿using System;
using System.Configuration;
using System.Web;

namespace Shuba.Web.master
{
    public partial class regional_plan_edit : basePage
    {
        string msg = "";

        private void BindData()
        {
            string RegionalID = Request.QueryString["id"];
            string TargetYear = Request.QueryString["yr"];

            Shuba.Business.RegionalTarget _RegionalTarget = new Shuba.Business.RegionalTarget();
            Shuba.Model.RegionalTargetInfo bi;

            bi = _RegionalTarget.Get(RegionalID, TargetYear);
            lblRegionalID.Text = bi.RegionalID;
            lblRegionName.Text = bi.RegionalName;
            lblYear.Text = bi.TargetYear;
            txtTargetAmount.Text = bi.TargetAmount.ToString();
            txtNoa.Text = bi.Noa.ToString();
            txtTargetAmountReal.Text = bi.TargetAmountReal.ToString();
            txtNoaReal.Text = bi.NoaReal.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtTargetAmount.ClientID;
            this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("TARGET_REGIONAL");
                if (!IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("TARGET_REGIONAL");

                if (Request.QueryString["a"] == "ed")
                {
                    BindData();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Shuba.Business.RegionalTarget _RegionalTarget = new Shuba.Business.RegionalTarget();
            Shuba.Model.RegionalTargetInfo bi = new Shuba.Model.RegionalTargetInfo();

            string ActType = "UPDATE";

            if (msg == "")
            {
                bi.RegionalID = lblRegionalID.Text;
                bi.TargetYear = lblYear.Text;
                bi.TargetAmount = Convert.ToInt64(txtTargetAmount.Text);
                bi.Noa = Convert.ToInt32(txtNoa.Text);
                bi.TargetAmountReal = Convert.ToInt64(txtTargetAmountReal.Text);
                bi.NoaReal = Convert.ToInt32(txtNoaReal.Text);
                bi.EntryBy = HttpContext.Current.User.Identity.Name;
                bi.EntryDate = ProcessingDate();
                bi.UpdateBy = HttpContext.Current.User.Identity.Name;
                bi.UpdateDate = ProcessingDate();
                bi.ActType = ActType;
                try
                {
                    _RegionalTarget.Save(bi);

                    RangeValidatorAmount.Visible = false;
                    RangeValidatorNoa.Visible = false;
                    RangeValidatorAmount2.Visible = false;
                    RangeValidatorNoa2.Visible = false;

                    AddLog(ActType, ActType + " REGION TARGET PLAN");

                    msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                    AlertAndRedirect(msg, "regional_plan_list.aspx");
                }
                catch (Exception ex)
                {
                    ErrBox.Visible = true;
                    ErrMsg.Text = ex.Message;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("regional_plan_list.aspx");
        }
    }
}
